c=document.getElementById('spielfeld');
ctx=c.getContext('2d');

//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
// Coordinates

//		 		0	1		2		3		4		5		6		7		8		9		10		11		12		13		14		15		16		17		18		19		20		21		22		23		24		25		26		27		28		29		30		31		32		33		34		35		36		37		38		39
var avatar_x = [12,	88,		131,	176,	221,	266,	309,	352,	397,	443,	489,	509,	509,	509,	509,	509,	509,	509,	509,	509,	509,	442,	397,	352,	307,	264,	221,	176,	130,	88,		12,		12,		12,		12,		12,		12,		12,		12,		12,		12];
var avatar_y = [12,	12,		12,		12,		12,		12,		12,		12,		12,		12,		12,		88,		132,	177,	222,	267,	310,	353,	398,	444,	488,	509,	488,	509,	509,	509,	509,	509,	509,	509,	509,	444,	398,	353,	310,	267,	222,	177,	132,	88];

//					0	1		2		3		4		5		6		7		8		9		10		11		12		13		14		15		16		17		18		19		20		21		22		23		24		25		26		27		28		29		30		31		32		33		34		35		36		37		38		39
var buidlings_x = [0,	88,		0,		176,	0,		0,		309,	0,		397,	443,	0,		489,	0,		489,	489,	0,		489,	0,		489,	498,	0,		442,	0,		352,	307,	0,		221,	176,	0,		88,		0,		67,		67,		0,		67,		0,		0,		67,		0,		67];
var buildings_y = [0,	67,		0,		67,		0,		0,		67,		0,		67,		67,		0,		88,		0,		177,	222,	0,		310,	0,		398,	444,	0,		488,	0,		488,	488,	0,		488,	488,	0,		488,	0,		444,	398,	0,		310,	0,		0, 		177,	0,		88];

//					0	1		2		3		4		5		6		7		8		9		10		11		12		13		14		15		16		17		18		19		20		21		22		23		24		25		26		27		28		29		30		31		32		33		34		35		36		37		38		39
var corners_x = [	12,	88,		131,	176,	221,	266,	309,	352,	397,	443,	489,	510,	0,		510,	510,	0,		510,	0,		510,	510,	0,		442,	0,		351,	306,	0,		220,	175,	0,		87,		0,		12,		12,		12,		12,		12,		12,		12,		12,		12];
var corners_y = [	12,	12,		12,		12,		12,		12,		12,		12,		12,		12,		12,		88,		0,		177,	222,	0,		310,	0,		398,	444,	0,		509,	0,		509,	509,	0,		509,	509,	0,		509,	0,		444,	398,	353,	310,	267,	222,	177,	132,	88];


//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
// Images

//---avatars-------------------------------------------------------------------------------------------------------------------------------------

var avatar0 = new Image();
avatar0.src = "gfx/avatars/0.png";
var avatar1 = new Image();
avatar1.src = "gfx/avatars/1.png";
var avatar2 = new Image();
avatar2.src = "gfx/avatars/2.png";
var avatar3 = new Image();
avatar3.src = "gfx/avatars/3.png";
var avatar4 = new Image();
avatar4.src = "gfx/avatars/4.png";
var avatar5 = new Image();
avatar5.src = "gfx/avatars/5.png";
var avatar6 = new Image();
avatar6.src = "gfx/avatars/6.png";
var avatar7 = new Image();
avatar7.src = "gfx/avatars/7.png";
var avatars = [avatar0,avatar1,avatar2,avatar3,avatar4,avatar5,avatar6,avatar7];

//---buildings----------------------------------------------------------------------------------------------------------------------------------

var hotel = new Image();
hotel.src = "gfx/buildings/hotel.png";
var house = new Image();
house.src = "gfx/buildings/house.png";

//---corners----------------------------------------------------------------------------------------------------------------------------------

var corner0h = new Image();
corner0h.src = "gfx/corners/0.png";
var corner1h = new Image();
corner1h.src = "gfx/corners/1h.png";
var corner2h = new Image();
corner2h.src = "gfx/corners/2h.png";
var corner3h = new Image();
corner3h.src = "gfx/corners/3h.png";
var corner4h = new Image();
corner4h.src = "gfx/corners/4h.png";
var corner5h = new Image();
corner5h.src = "gfx/corners/5h.png";
var corner6h = new Image();
corner6h.src = "gfx/corners/6h.png";
var corner7h = new Image();
corner7h.src = "gfx/corners/7h.png";

var corner0v = new Image();
corner0v.src = "gfx/corners/0v.png";
var corner1v = new Image();
corner1v.src = "gfx/corners/1v.png";
var corner2v = new Image();
corner2v.src = "gfx/corners/2v.png";
var corner3v = new Image();
corner3v.src = "gfx/corners/3v.png";
var corner4v = new Image();
corner4v.src = "gfx/corners/4v.png";
var corner5v = new Image();
corner5v.src = "gfx/corners/5v.png";
var corner6v = new Image();
corner6v.src = "gfx/corners/6v.png";
var corner7v = new Image();
corner7v.src = "gfx/corners/7v.png";
var corners = [
               [corner0h,corner1h,corner2h,corner3h,corner4h,corner5h,corner6h,corner7h],
               [corner0v,corner1v,corner2v,corner3v,corner4v,corner5v,corner6v,corner7v]
              ];

//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
// Functions

function moveAvatar(avatar,field){
	
	if (field < 40) {		
		var xpos = avatar_x[field] + 5;
		var ypos = avatar_y[field] + 5;
		
		var limit = 0;
		if ((field >= 0 && field < 10) || (field >= 20 && field < 30))
			limit = 3;
		else if ((field >= 10 && field < 20) || (field >= 30 && field < 40))
			limit = 4;
		
		xpos = xpos + (avatar % limit) * 12;
		if (avatar >= 2*limit)
			ypos = ypos + 30;
		else if (avatar >= limit)
			ypos = ypos + 15;
		
		ctx.drawImage(avatars[avatar], xpos, ypos,10,15);
	}
}

//---------------------------------------------------------------------------------------------------------------------------------------------

function setCorner(cornerNumber,field){
	
	var xpos = corners_x[field];
	var ypos = corners_y[field];
	
	if (field == 1 || field == 3 || field == 6 || field == 8 || field == 9 || field == 21 || field == 23 || field == 24 || field == 26 || field == 27 || field == 29) {
		ctx.drawImage(corners[1][cornerNumber], xpos, ypos);
	} else if (field == 11 || field == 13 || field == 14 || field == 16 || field == 18 || field == 31 || field == 32 || field == 34 || field == 37 || field == 39) {
		ctx.drawImage(corners[0][cornerNumber], xpos, ypos);
	}
}

//---------------------------------------------------------------------------------------------------------------------------------------------

function setBuildings(num,field){
	var offseth = 11;
	var offsetv = 11;
	
	var xpos = buidlings_x[field];
	var ypos = buildings_y[field];
	
	if (field == 1 || field == 3 || field == 6 || field == 8 || field == 9 || field == 21 || field == 23 || field == 24 || field == 26 || field == 27 || field == 29) {
		if (num <= 4) {
			for (var i = 0; i<num; i++) {
				ctx.drawImage(house, xpos+i*offsetv, ypos,11,11);
			}
		} else if (num == 5) {
			ctx.drawImage(hotel, xpos+offsetv, ypos,19,19);
		}
	} else if (field == 11 || field == 13 || field == 14 || field == 16 || field == 18 || field == 31 || field == 32 || field == 34 || field == 37 || field == 39) {
		if (num <= 4) {
			for (var i = 0; i<num; i++) {
				ctx.drawImage(house, xpos, ypos+i*offseth,11,11);
			}
		} else if (num == 5) {
			ctx.drawImage(hotel, xpos, ypos+offseth,19,19);
		}
	}
}

//---------------------------------------------------------------------------------------------------------------------------------------------

function clearField() {
	ctx.clearRect(0, 0, 575, 575);
}